$:.unshift File.join(File.dirname(__FILE__),'..','lib')

require 'test/unit'
require 'graphics_oop'

class Test_Graphics < Test::Unit::TestCase
   def test_point()
      assert_equal(Point2d[0,0],Point2d[0,0])
      assert_equal(2,Point2d[2,1].first)
      assert_equal(1,Point2d[2,1].last)
      assert_equal(true,Point2d[2,1]==Point2d[2,1])
      assert_equal(false,Point2d[2,2]==Point2d[2,1])
   end
   def test_range1d()
      assert_equal(Range1d[0,0],Range1d[0,0])
      assert_equal(1,Range1d[1,2].first)
      assert_equal(2,Range1d[1,2].last)
      assert_equal(true,Range1d[1,2]==Range1d[1,2])
      assert_equal(false,Range1d[2,2]==Range1d[1,2])
      assert_equal(Range1d[2,4],Range1d[2,3]+Range1d[3,4])
   end
   def test_range2d()
      assert_equal(Range2d[Range1d[0,0],Range1d[0,0]],Range2d[Range1d[0,0],Range1d[0,0]])
      assert_equal(Range1d[1,1],Range2d[Range1d[1,1],Range1d[2,2]].first)
      assert_equal(Range1d[2,2],Range2d[Range1d[1,1],Range1d[2,2]].last)
      assert_equal(true,Range2d[Range1d[1,1],Range1d[2,2]]==Range2d[Range1d[1,1],Range1d[2,2]])
      assert_equal(false,Range2d[Range1d[1,1],Range1d[2,2]]==Range2d[Range1d[1,1],Range1d[2,3]])
      assert_equal(Range2d[Range1d[1,1],Range1d[1,1]],Range2d[Range1d[0,0],Range1d[0,0]].translate(1))
#      assert_equal(Range2d[Range1d[0,0],Range1d[0,0]],Range2d[Range1d[0,0],Range1d[0,0]]+Range2d[Range1d[0,0],Range1d[0,0]])
      #assert_equal(Range2d[Range1d[10,50],Range1d[5,20]],Range2d[Range1d[10,20],Range1d[10,20]]+Range2d[Range1d[30,50],Range1d[5,10]])
      
      #assert_equal(Range2d[Range1d[1,1],Range1d[1,1]],Range2d[Range1d[0,0],Range1d[0,0]] + 1)
 end
   def test_union1d
      range0 = Range1d[10,10]
      range1 = Range1d[11,12]
      range2 = Range1d[12,13]
      range3 = Range1d[1,2]
      range4 = Range1d[2,3]

      union1 = Union1d[range1,range2]
      union2 = Union1d[range3,range4]

      assert_equal(Union1d[range1,range2],Union1d[range1,range2])
      assert_equal(Union1d[union1,range2],Union1d[union1,range2])
      assert_equal(true,Union1d[union1,range2]==Union1d[union1,range2])
      #Translation
      assert_equal(Union1d[range1,range2],Union1d[range3,range4].translate(10))
      assert_equal(Union1d[range3,range4],Union1d[range1,range2].translate(-10))
      assert_raise (RuntimeError) {Union1d[range1,range2].translate("TEST")}
      
     #assert_equal(Union1d[range1,range2],Union1d[range3,range4] + 10)
     #assert_equal(Union1d[range3,range4],Union1d[range1,range2] + -10 )
   end
   def test_union2d
      range1 = Range1d[11,12]
      range2 = Range1d[12,13]
      range3 = Range1d[1,2]
      range4 = Range1d[2,3]
      range2d1 = Range2d[range1,range2]
      range2d2 = Range2d[range3,range4]
      union  = Union1d[range1,range2]
      union2 = Union2d[range2d1,range2d2]
      union22 = Union2d[range2d1.translate(10),range2d2.translate(10)]
      range22 = Range1d[12,13].translate(10)
      
     assert_equal(Union2d[range2d1,range2d2],Union2d[range2d1,range2d2])
     assert_equal(Union2d[union2,range2],Union2d[union2,range2])
     assert_equal(true,Union2d[union2,range2]==Union2d[union2,range2])
     #Translation
     assert_equal(Union2d[union22,range22],Union2d[union2,range2].translate(10))
     assert_equal(Union2d[union2,range2],Union2d[union22,range22].translate(-10))
  
     #assert_equal(Union2d[union22,range22],Union2d[union2,range2] + 10)
     #assert_equal(Union2d[union2,range2],Union2d[union22,range22] + -10)
   end
   def test_bounding_range
     range1  = Range1d[12,16]
     range2  = Range1d[22,28]
     range3  = Range1d[32,35]
     range4  = Range1d[42,44]
     range2d1 = Range2d[range1,range2]
     range2d2 = Range2d[range3,range4]
     union  = Union1d[range1,range2]
     union2 = Union2d[range2d1,range2d2] 
      
     assert_equal(Range1d[12,28],union.bounds)
     assert_equal(Range1d[12,28],range1.bounding_range(range2))
     assert_equal(Range2d[Range1d[12,35],Range1d[22,44]],range2d1.bounding_range(range2d2))
     assert_equal(Range2d[Range1d[12,35],Range1d[22,44]],union2.bounds)
   end
   
   def test_in_shape
     range1  = Range1d[1,4]
     range2  = Range1d[2,6]
     range3  = Range1d[7,12]
     range4  = Range1d[13,19]
     range2d1  = Range2d[range1,range2]
     range2d2  = Range2d[range3,range4]
     union  = Union1d[range1,range2]
     union2 = Union2d[range2d1,range2d2] 
   
     point1 = 5
     point2 = 8
     point3 = Point2d[8,43]
       
     assert_equal(true,union.in_shape?(point1))
     assert_equal(false,range1.in_shape?(point1))
     assert_equal(false,union2.in_shape?(point3))
     assert_equal(false,range1.in_shape?(point2))
  end
   def test_arithmetric
      r1 = Range2d[Range1d[0,2],Range1d[2,5]]
      r2 = Range2d[Range1d[-1,1],Range1d[-1,1]]
      r3 = Range2d[Range1d[1,1],Range1d[1,1]]
      assert_equal(true,Diff2d[Union2d[r1,r2],r3]==Diff2d[Union2d[r1,r2],r3])
      assert_equal(true,Union2d[r1,r2]==r1+r2)
      assert_equal(true,Diff2d[Union2d[r1,r2],r3]==r1+r2-r3)
   end
   def test_graph_equal_trans
     range1  = Range1d[12,16]
     range2  = Range1d[22,28]
     range3  = Range1d[32,36]
     range4  = Range1d[42,48]
     range2d1 = Range2d[range1,range2]
     range2d2 = Range2d[range3,range4]
     union  = Union1d[range1,range2]
     union2 = Union2d[range2d1,range2d2] 
   
     range5  = Range1d[52,56]
     range6  = Range1d[62,68]
     range7  = Range1d[72,76]
     range8  = Range1d[82,88]
     range2d3 = Range2d[range5,range6]
     range2d4 = Range2d[range7,range8]
     union3  = Union1d[range5,range6]
     union4 = Union2d[range2d3,range2d4] 
   
     
     assert_equal(true,range1.graph_equal_trans?(range3))
     assert_equal(true,range2d1.graph_equal_trans?(range2d2))
     assert_equal(true,union3.graph_equal_trans?(union))
     assert_equal(true,union4.graph_equal_trans?(union2))
  end
  def test_to_s
     range1  = Range1d[12,16]
     range2  = Range1d[22,28]
     range3  = Range1d[32,36]
     range4  = Range1d[42,48]
     range2d1 = Range2d[range1,range2]
     range2d2 = Range2d[range3,range4]
     union  = Union1d[range1,range2]
     union2 = Union2d[range2d1,range2d2] 
     
     assert_equal('Range1d[12,16]',range1.to_s)
     assert_equal('Range2d[Range1d[12,16],Range1d[22,28]]',range2d1.to_s)
     assert_equal('Union1d[Range1d[12,16],Range1d[22,28]]',union.to_s)
     assert_equal('Union2d[Range2d[Range1d[12,16],Range1d[22,28]],Range2d[Range1d[32,36],Range1d[42,48]]]',union2.to_s)
     
  end  
end
